<template>
	<view class="container">
		<image class="cover" :src="data.cover" />
		<x-button class="btn" title="预览" @click="previewClick"></x-button>
		<x-button class="btn" title="跟别人分享吧" @click="shareClick"></x-button>
		<x-button class="btn" title="让老师帮忙打印" @click="requestPrint"></x-button>

		<u-action-sheet :list="list" v-model="show" @click="actionClick"></u-action-sheet>
	</view>
</template>

<script>
	// #ifdef H5
	const jweixin = require('jweixin-module')
	// #endif
	import XButton from '@/components/x-button.vue'
	import { requestPrintAlbumTemplate } from '@/api/album.js'
	export default {
		components: {
			XButton
		},
		data() {
			return {
				data: {},

				list: [{
					text: '微信好友',
				}, {
					text: '微信朋友圈'
				}, {
					text: '复制链接'
				}],
				show: false,
				title: '分享'
			}
		},
		onLoad(options) {
			console.log(options);
			this.data = options
			// #ifdef H5
				if (this.isWeixin()) {
					jweixin.ready(function() {
						console.log('ready');
						this.wxReady = true;
						this.setWxShare()
					})
					this.initWxConfig();
				}
			// #endif
		},
		methods: {
			previewClick() {
				this.$u.route({
					url: '/pages_base/common/webview',
					params: {
						url: this.data.staticURL,
					}
				})
			},

			shareClick() {
				console.log('123456')
				this.show = true
			},
			actionClick(index) {
				console.log(index)
				let scene = 'WXSceneSession'
				if (index === 0) {
					scene = 'WXSceneSession'
				} else if (index === 1) {
					scene = 'WXSceneTimeline'
				} else if (index === 2) {
					uni.setClipboardData({
						data: this.data.staticURL,
						success: () => {
							console.log('success');
							this.$u.toast('链接复制成功')
						}
					});
					return
				}

				uni.share({
					provider: "weixin",
					scene: scene,
					type: 0,
					href: this.data.staticURL,
					title: `${this.data.name}的相册`,
					summary: `您有一封宝宝的时光相册，请查收`,
					imageUrl: this.data.cover,
					success: function(res) {
						console.log("success:" + JSON.stringify(res));
					},
					fail: function(err) {
						console.log("fail:" + JSON.stringify(err));
					}
				});
			},
			
			requestPrint() {
				requestPrintAlbumTemplate({albumId: this.data.id}).then(res => {
					this.$u.toast('请求打印成功，等待老师联系')
					this.$u.route({
						url: '/pages_base/feed/album-template-pay',
						params: {
							...this.data
						}
					})
				})
			},

			initWxConfig() {
				getWxConfig(window.location.href).then(res => {
					console.log(res);
					if (res.code === '000000') {
						jweixin.config({
							debug: false,
							appId: res.data.appId,
							timestamp: res.data.timestamp,
							nonceStr: res.data.nonceStr,
							signature: res.data.signature,
							jsApiList: [
								'chooseWXPay',
							]
						})
					} else {
						this.$u.toast('微信参数获取失败:' + res.message);
					}
				})
			},

			setWxShare() {
				jweixin.updateAppMessageShareData({
					title: '', // 分享标题
					desc: '', // 分享描述
					link: '', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imgUrl: '', // 分享图标
					success: function() {
						// 设置成功
					}
				})

				jweixin.updateTimelineShareData({
					title: '', // 分享标题
					link: '', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imgUrl: '', // 分享图标
					success: function() {
						// 设置成功
					}
				})
			},
			
			isWeixin() {
				const ua = navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == 'micromessenger') {
					return true;
				} else {
					return false;
				}
			},
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;

		.cover {
			margin-top: 90rpx;
			width: 375rpx;
			height: 667rpx;
		}

		.btn {
			margin-top: 40rpx;
		}
	}
</style>